package cn.iocoder.yudao.module.member.api.config;

import cn.iocoder.yudao.module.member.api.config.dto.MemberConfMaterialDTO;
import cn.iocoder.yudao.module.member.api.config.dto.MemberConfRespDTO;
import cn.iocoder.yudao.module.member.convert.config.MemberConfConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.memberconf.MemberConfDO;
import cn.iocoder.yudao.module.member.dal.dataobject.memberconf.confadditionalfee.MemberConfAdditionalFeeDO;
import cn.iocoder.yudao.module.member.dal.dataobject.memberconf.confmaterial.MemberConfMaterialDO;
import cn.iocoder.yudao.module.member.dal.dataobject.memberconf.confpoint.MemberConfPointDO;
import cn.iocoder.yudao.module.member.dal.dataobject.memberconf.confrebate.MemberConfRebateDO;
import cn.iocoder.yudao.module.member.service.memberconf.MemberConfService;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class MemberConfApiImpl implements MemberConfApi {
    @Resource
    private MemberConfService memberConfService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MemberConfRespDTO createMemberConfByDefault(Long userId) {
        MemberConfDO confDO = memberConfService.getConfByUser(userId);
        if (confDO != null) {
            return getMemberConfByUserId(userId);
        }

        // 获取默认配置，没有就报错
        confDO = memberConfService.getConfDefault();
        // 获取物料策略配置
        List<MemberConfMaterialDO> materialList = memberConfService.getConfMaterialListByMemberConfId(confDO.getId());
        confDO.setId(null);
        confDO.setUserId(userId);
        // 给用户配置默认配置
        memberConfService.saveConf(confDO);
        Long confId = confDO.getId();
        List<MemberConfMaterialDTO> materialDTOList = new ArrayList<>(materialList.size());

        // 处理物料策略的子表关联
        for (MemberConfMaterialDO materialDO : materialList) {
            // 获取现有配置
            Long originMaterialConfId = materialDO.getId();
            List<MemberConfPointDO> confPointList = memberConfService.getConfPointListByMaterialId(originMaterialConfId);
            List<MemberConfRebateDO> confRebateList = memberConfService.getConfRebateListByMaterialId(originMaterialConfId);
            List<MemberConfAdditionalFeeDO> confAdditionalFeeList = memberConfService.getConfAdditionalFeeListByMaterialId(originMaterialConfId);
            // 保存物料配置
            materialDO.setId(null);
            materialDO.setMemberConfId(confId);
            memberConfService.saveMaterialConf(materialDO);
            Long materialConfId = materialDO.getId();

            // 创建关联的积分规则
            confPointList = memberConfService.createMemberConfPointList(materialConfId, confPointList);

            // 创建关联的返佣政策
            confRebateList = memberConfService.createMemberConfRebateList(materialConfId, confRebateList);


            // 创建关联的阶梯费用
            confAdditionalFeeList = memberConfService.createMemberConfAdditionalFeeList(materialConfId, confAdditionalFeeList);
            materialDTOList.add(MemberConfConvert.INSTANCE.convertMaterialDTO(materialDO, confPointList, confRebateList, confAdditionalFeeList));
        }

        // 获取其他配置项给用户配置
        return MemberConfConvert.INSTANCE.convertDTO(confDO, materialDTOList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Cacheable(cacheNames = "member_conf_cache", key = "#userId")
    public MemberConfRespDTO getMemberConfByUserId(Long userId) {
        // 获取用户配置 没有就根据默认配置创建
        MemberConfDO confDO = memberConfService.getConfByUser(userId);
        if (confDO == null) {
            confDO = memberConfService.getConfDefault();
            this.createMemberConfByDefault(userId);
        }
        // 获取物料策略配置
        List<MemberConfMaterialDO> materialList = memberConfService.getConfMaterialListByMemberConfId(confDO.getId());
        List<MemberConfMaterialDTO> materialDTOList = new ArrayList<>(materialList.size());
        // 处理物料策略的子表关联
        for (MemberConfMaterialDO materialDO : materialList) {
            // 获取现有配置
            Long materialConfId = materialDO.getId();
            List<MemberConfPointDO> confPointList = memberConfService.getConfPointListByMaterialId(materialConfId);
            List<MemberConfRebateDO> confRebateList = memberConfService.getConfRebateListByMaterialId(materialConfId);
            List<MemberConfAdditionalFeeDO> confAdditionalFeeList = memberConfService.getConfAdditionalFeeListByMaterialId(materialConfId);
            materialDTOList.add(MemberConfConvert.INSTANCE.convertMaterialDTO(materialDO, confPointList, confRebateList, confAdditionalFeeList));
        }
        return MemberConfConvert.INSTANCE.convertDTO(confDO, materialDTOList);
    }

    @Override
    public MemberConfRespDTO getDefaultConf() {
        MemberConfDO confDO = memberConfService.getConfDefault();
        return MemberConfConvert.INSTANCE.convertDTO(confDO);
    }

}
